package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Category;
import com.aaa.vegetablemarketexpress.service.CategoryService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: CategoryController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/6 15:05
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("category")
@Log4j2
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Category> page) {
        log.info("开始执行分页查询");
        PageInfo pageInfo = categoryService.queryByPage(page);
        log.info("分页查询结束");
        return ResultUtil.success(pageInfo);
    }
    /**
     * 根据id查询
     * @param categoryId
     * @return
     */
    @GetMapping("queryByCategoryId")
    public Result queryByCategoryId(Long categoryId) {
        return ResultUtil.success(categoryService.queryByCategoryId(categoryId));
    }

    /**
     * 添加
     * @param category
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Category category) {
        return ResultUtil.success(categoryService.add(category));
    }

    /**
     * 更新
     * @param category
     * @return
     */
    @PostMapping("update")

    public Result update(@RequestBody Category category) {
        return ResultUtil.success(categoryService.update(category));
    }

    /**
     * 根据id删除
     * @param categoryId
     * @return
     */
    @DeleteMapping("deleteByCategoryId")
    public Result deleteBycategoryId(Long categoryId) {
        return ResultUtil.success(categoryService.deleteBycategoryId(categoryId));
    }

    /**
     * 查询分类树形结构
     * @return
     */
    @GetMapping("queryCategoryTree")
    public Result queryCategoryTree() {
        log.info("开始查询分类树形结构");
        List<Category> categoryList = categoryService.queryCategoryTree();
        log.info("分类树形结构查询结束，共{}条数据", categoryList.size());
        return ResultUtil.success(categoryList);
    }
}
